package com.jlnk.androidfmc.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.view.NyqPost;

import java.util.ArrayList;

public class LatestFragment extends Fragment {

    private ListView latestList;
    private ArrayList<Item> items;

    // 定义发布按钮的点击事件监听器
    private View.OnClickListener releaseButtonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 跳转到发布页面
            goToNyqPostActivity();
        }
    };
    public LatestFragment() {
        // Required empty public constructor
    }

    public static LatestFragment newInstance() {
        LatestFragment fragment = new LatestFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Initialize items if needed
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_latest, container, false);

        latestList = view.findViewById(R.id.latestList);

        items = new ArrayList<>();
        items.add(new Item("张三", "yhm", "n3", "张三喜欢学习的叭？？？？"));
        LatestAdapter adapter = new LatestAdapter(getActivity(), items);
        latestList.setAdapter(adapter);

        return view;
    }

    private class LatestAdapter extends ArrayAdapter<Item> {
        public LatestAdapter(android.content.Context context, ArrayList<Item> items) {
            super(context, 0, items);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_latest, parent, false);
            }

            Item item = getItem(position);
            ImageView icon = convertView.findViewById(R.id.icon);
            TextView username = convertView.findViewById(R.id.username);
            TextView content = convertView.findViewById(R.id.content);
            ImageView image = convertView.findViewById(R.id.image);

            // Set the data into views
            icon.setImageResource(getResources().getIdentifier(item.getIconName(), "drawable", getActivity().getPackageName()));
            username.setText(item.getUsername());
            content.setText(item.getContent());
            image.setImageResource(getResources().getIdentifier(item.getImageName(), "drawable", getActivity().getPackageName()));

            return convertView;
        }
    }

    // Your data model class
    public static class Item {
        private String username;
        private String iconName;
        private String imageName;
        private String content;

        public Item(String username, String iconName, String imageName, String content) {
            this.username = username;
            this.iconName = iconName;
            this.imageName = imageName;
            this.content = content;
        }

        public String getUsername() {
            return username;
        }

        public String getIconName() {
            return iconName;
        }

        public String getImageName() {
            return imageName;
        }

        public String getContent() {
            return content;
        }
    }
    private void goToNyqPostActivity() {
        // 使用 requireContext 来确保 Fragment 已经附加到 Activity
        Intent intent = new Intent(requireContext(), NyqPost.class);
        startActivity(intent);
    }

}